// pages/list/list.js
const app = getApp();
var myfoodlist = null;
var mylist = null;
var intervalId;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    foodlist: ["蛋炒饭", "油泼面", "烧烤", "炸鸡汉堡", "川菜", "粤菜", "湘菜", "海鲜", "火锅", "自助餐", "饺子", "炸串", "麻辣烫", "自己做的饭", "猪脚面", "快餐", "炒面炒粉", "红烧肉", "米线", "咖喱饭", "方便面", "面包", "牛肉汤", "烧茄子", "糖醋排骨", "螺蛳粉", "路边摊", "拉面", "酸菜鱼"],
    count: 0,
    eat: "",
    randomnum: null,
    hiddenmodalput: true,
    start: true,
    intervalId: null, // 存储定时器的ID
    isPlaying: false, // 标记是否正在播放
  },

  showrandomfood() {
    if (this.data.isPlaying) {
      // 如果正在播放，则停止播放
      clearInterval(this.data.intervalId);
      this.setData({
        isPlaying: false,
      });
    } else {
      // 如果未播放，则开始播放
      const intervalId = setInterval(() => {
        const randomIndex = Math.floor(Math.random() * this.data.foodlist.length);
        const foodName = this.data.foodlist[randomIndex];
        this.setData({
          eat: foodName,
        });
      }, 60);

      this.setData({
        intervalId,
        isPlaying: true,
      });
    }
  },

  cancelchange() {
    this.setData({ hiddenmodalput: true })
  },
  showfoodlist() {
    this.setData({ hiddenmodalput: false })
  },
  btnHandler(e) {
    if (this.data.isPlaying) {
      // 如果正在播放，则停止播放
      clearInterval(this.data.intervalId);
      this.setData({
        isPlaying: false,
      });
    } else {
      // 如果未播放，则开始播放
      const intervalId = setInterval(() => {
        const randomIndex = Math.floor(Math.random() * this.data.foodlist.length);
        const foodName = this.data.foodlist[randomIndex];
        this.setData({
          eat: foodName,
        });
      }, 1000);

      this.setData({
        intervalId,
        isPlaying: true,
      });
    }
  },
  foodinput(e) {
    myfoodlist = e.detail.value
    console.log(myfoodlist)
    mylist = myfoodlist.split(',')
  },
  changefoodlist() {
    wx.cloud.callFunction({
      name: 'randomfood',
      data: {
        foodlist: mylist
      },
      success: res => {
        const { eat } = res.result;
        this.setData({ foodlist: mylist, eat });
        try {
          wx.setStorageSync('foodlist', mylist);
        } catch (e) {
          console.log(e);
        } finally {
          this.setData({ hiddenmodalput: true });
        }
      },
      fail: err => {
        console.error(err);
      }
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let a = Math.floor(Math.random() * (this.data.foodlist.length - 1));
    this.setData({ randomnum: a });
    wx.showShareMenu({
      withShareTicket: true,
      menus: ["shareAppMessage", "shareTimeline"]
    });

    try {
      var value = wx.getStorageSync('foodlist');
      if (value) {
        this.setData({ foodlist: value });
      } else {
        wx.setStorageSync('foodlist', this.data.foodlist);
      }
    } catch (e) {
      console.log(e);
    }

    // 调用云函数初始化页面的食物名称
    wx.cloud.callFunction({
      name: 'randomfood',
      data: {
        foodlist: this.data.foodlist
      },
      success: res => {
        const { eat } = res.result;
        this.setData({ eat });
      },
      fail: err => {
        console.error(err);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    wx.showToast({
      title: '分享成功',
      icon: 'success',
      duration: 1000//持续的时间
    })
  }
})
